package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.config.R;
import com.itheima.config.ThreadLocals;
import com.itheima.pojo.Employee;
import com.itheima.service.employeeService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.sql.SQLIntegrityConstraintViolationException;
import java.time.LocalDateTime;
import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * 员工管理
 */

@Slf4j
@RestController
@RequestMapping("/employee")
public class employeeController {

    @Autowired
    private employeeService employeeService;

    /**
     * 登录功能
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpServletRequest request) {

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());

        //通过 用户名字拿到  是否有相同账号的
        Employee one = employeeService.getOne(queryWrapper);


        //获取用户传的密码
        String password = employee.getPassword();
        //MD5方式给密码加密
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(password.getBytes());


        if (one == null){
            return R.error("账号不存在");
        }

        if (!md5DigestAsHex.equals(one.getPassword())) {
            return R.error("密码错误");
        }

        if (one.getStatus() == 0){
            return R.error("账号已经禁用");
        }

        HttpSession session = request.getSession();
        session.setAttribute("one",one.getId());
        return R.success(one);
    }

    /**
     * 退出功能
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R logOut(HttpServletRequest request){

        HttpSession session = request.getSession();
        session.removeAttribute("one");

        return R.success("退出成功");
    }

    /**
     * 分页查询和模糊查询
     * @param page 第几页
     * @param pageSize 每页多少条数据
     * @param name 名称
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        //分页对象
        Page<Employee> employeePage = new Page<>(page,pageSize);

        //模糊查询   name为空 将查询全部 就不带模糊查询
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getUsername,name);

        //分页查询 带  模糊 查询条件
        employeeService.page(employeePage,queryWrapper);

        return R.success(employeePage);
    }

    /**
     * 添加员工信息
     * @return
     */
    @PostMapping
    public R save(@RequestBody Employee employee){

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());

        // 根据名称查询数据库是否存在这个数据
        employeeService.getOne(lambdaQueryWrapper);

        String md5DigestAsHex = DigestUtils.md5DigestAsHex("123456".getBytes());

        //数据库不存在这个人就进行添加数据   已经不需要手动添加数据  自动填充已经帮这些字段赋值了
        // 从Session中获取id 确定当前操作人
//       Long attribute = (Long) httpServletRequest.getSession().getAttribute("one");

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser(attribute);
//        employee.setUpdateUser(attribute);


          employee.setPassword(md5DigestAsHex);

        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 查询回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> echo(@PathVariable Long id){
        // 通过ID 查询到数据
        Employee byId = employeeService.getById(id);
        // 点击编辑的时候框里显示数据
        return R.success(byId);
    }

    /**
     * 员工的禁用和启用
     * @param employee
     * @return
     */
    @PutMapping
    public R disable(@RequestBody Employee employee){

        //获取 Session 中修改人的ID     已经从公共方法获取ID
      //  Long one = (Long) httpServletRequest.getSession().getAttribute("one");

        //用公共方法获取ID
        Long one = (Long)ThreadLocals.getthread().get();

        // 修改人
        employee.setUpdateUser(one);

        // 修改当前时间   自动填充 已经不需要写了
       // employee.setUpdateTime(LocalDateTime.now());

        //修改数据
        employeeService.updateById(employee);

        return R.success("修改成功");
    }


}
